﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class TblBangGiaDAO:IData<TblBangGia> 
 	{
		public TblBangGiaDAO(String myconnecString) : base(myconnecString) { }

		protected override int InsertOnSubmit(ref TblBangGia item, int t) {
			 SqlParameter p = new SqlParameter(); 
			 if( t == 0)
				p.Direction = ParameterDirection.Output;
			 p.ParameterName = "@" + TblBangGia.c_id;
			 p.Value = item.id;
			 Object obj = item.id; 
			int i = connec.ExecuteNonQuery("sp_TblBangGia", CommandType.StoredProcedure,p,ref obj,new SqlParameter("@flag", t),
				 new SqlParameter("@" + TblBangGia.c_Tu, item.Tu), new SqlParameter("@" + TblBangGia.c_Den, item.Den),
				 new SqlParameter("@" + TblBangGia.c_DonGia, item.DonGia), new SqlParameter("@" + TblBangGia.c_Ngay, item.Ngay));
			if(t == 0)
				item.id = Convert.ToInt32(obj);
			return i;
		}

		public override TblBangGia GetItem(IDataReader dr)
		{
			TblBangGia obj = new TblBangGia();
			obj.id = Convert.ToInt32(dr[TblBangGia.c_id]);
			obj.Tu = Convert.ToDouble(dr[TblBangGia.c_Tu]);
			obj.Den = Convert.ToDouble(dr[TblBangGia.c_Den]);
			obj.DonGia = Convert.ToDouble(dr[TblBangGia.c_DonGia]);
			obj.Ngay = Convert.ToDateTime(dr[TblBangGia.c_Ngay]);
			return obj;
		}

		public override List<TblBangGia> GetAll()
		{
			return GetAll("sp_GetAllTblBangGia", CommandType.StoredProcedure);
		}

		public override TblBangGia GetItemById(object id)
		{
			return GetAll().SingleOrDefault(p => p.id == Convert.ToInt32(id));
		}
		public override TblBangGia GetItemById(TblBangGia item){
			throw new NotImplementedException();
		}

	}

 }
